package com.snowfeel.sfmall.sys.controller;

import com.snowfeel.sfmall.core.enums.ResponseCode;
import com.snowfeel.sfmall.core.response.JsonResult;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.dto.SysPermissionParam;
import com.snowfeel.sfmall.sys.query.SysPermissionQuery;
import com.snowfeel.sfmall.sys.service.SysPermissionService;
import com.snowfeel.sfmall.sys.vo.SysPermissionNode;
import com.snowfeel.sfmall.sys.vo.SysPermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 后台用户权限管理
 * Created on 2018/9/29.
 */
@RestController
@Api(tags = "SysPermissionController", description = "后台用户权限管理")
@RequestMapping("/permission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @ApiOperation("添加权限")
    @PostMapping("/create")
    public JsonResult create(@RequestBody SysPermissionParam permission) {
        int count = permissionService.create(permission);
        if(count>0){
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("修改权限")
    @PostMapping("/update/{id}")
    public JsonResult update(@PathVariable Long id, @RequestBody SysPermissionParam permission) {
        int count = permissionService.update(id, permission);
        if(count>0){
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("根据id批量删除权限")
    @PostMapping("/delete")
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = permissionService.delete(ids);
        if(count>0){
            return JsonResult.success(count);
        }
        return JsonResult.error(ResponseCode.FAIL);
    }

    @ApiOperation("以层级结构返回所有权限")
    @GetMapping("/treeList")
    public JsonResult<List<SysPermissionNode>> treeList() {
        List<SysPermissionNode> permissionNodeList = permissionService.treeList();
        return JsonResult.success(permissionNodeList);
    }

    @ApiOperation("获取所有权限列表")
    @PostMapping("/page")
    public JsonResult<PageVo<SysPermissionVo>> page(@RequestBody SysPermissionQuery query) {
        PageVo<SysPermissionVo> permissionList = permissionService.page(query);
        return JsonResult.success(permissionList);
    }
}
